#include using namespace std; class base{ private: double d; public: double pub; base():d(1.7){} void setD(double pd){d = pd;} double getD()const{return d;} friend ostream& operator<<(ostream o, base b); }; ostream& operator<<(ostream &o, base b){ o << b.getD(); return o; } // default inheritance is private class child1:base{ public: // how to call the base class constructor child1():base(){} // not allowed to access d from base //void setD(double pd){d = pd;} // allowed to access public variables from base void setPub(double ppub){pub = ppub;} }; // public can access all public class child2: public base{ public: child2():base(){} }; // can access all public data class child3: protected base{ public: child3():base(){} }; // can access all public data class child4: protected base { public: child4():base(){} // child4 can access getD, but main can not double goGetD(){return getD();} }; void main(){ base b; cout << b << endl; b.setD(3.14159); cout << b << endl; child1 c1; // does not work, default inheritance is private //cout << c1.getD() << endl; child2 c2; cout << c2.getD() << endl; child3 c3; // does not work, because of protected keyword //cout << c3.getD() << endl; child4 c4; // does not work, because of protected keyword cout << c4.goGetD() << endl; }